package com.east.sng.dao;

import com.east.sng.entity.param.CourseParam;
import com.east.sng.entity.result.CourseFind;
import com.east.sng.entity.result.CourseInfo;
import com.east.sng.utils.RedisCache;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author east
 * @date: 2020/12/29
 * @Description:
 * @Version: 1.0
 **/
@Repository
@CacheNamespace(implementation = RedisCache.class)
public interface CourseDao {

    /**
     * 查询全部课程信息
     * @param params 课程参数列表
     * @return 课程信息实例对象列表
     */
    List<CourseInfo> selectCourses(Map<String, Object> params);

    /**
     * 查询全部课程的id和名称
     * @return 课程id和名称的实例列表
     */
    @Select("select course_id courseId,course_name courseName from course ")
    List<Map<String,String>> selectCourseColumns();

    /**
     * 根据课程id查询
     * @param courseId 课程id
     * @return 课程信息实例对象
     */
    @Select("select c.course_id,c.course_name,c.course_duration,c.course_price,c.min_age,c.max_age,"
            +"ct.course_typeid,ct.course_typename,cr.classroom_name,t.teacher_name "
            + "from course  c "
            + "left join  course_type  ct  on c.course_typeid = ct.course_typeid "
            + "left join  classroom  cr  on  c.classroom_id = cr.classroom_id "
            + "left join  teacher  t  on  c.teacher_id = t.teacher_id "
            + "where c.flag = 0 and c.course_id = #{courseId} ")
    CourseFind selectCourse(Integer courseId);

    /**
     * 根据课程类型查询授课教室
     * @param courseTypeId 课程类型id
     * @return 教室id和名称的实例列表
     */
    @Select("select distinct cr.classroom_id classroomId,cr.classroom_name classroomName "
            + "from  classroom cr "
            + "left join course  c on  c.classroom_id = cr.classroom_id "
            + "where c.flag = 0 and c.course_typeid=#{courseTypeId} ")
    List<Map<String, Object>> selectClassrooms(Integer courseTypeId);

    /**
     * 添加课程
     * @param courseParam 课程对象
     */
    @Insert("insert  into  course "
            + "values(null,#{course.courseName},#{course.courseDuration},#{course.coursePrice},#{course.minAge},#{course.maxAge},"
            + "#{course.courseTypeName},#{course.classroomName},#{course.teacherName}) ")
    void insertCourse(@Param("course") CourseParam courseParam);

    /**
     * 修改课程
     * @param courseParam 课程对象
     */
    @Update("update course set course_name=#{course.courseName},course_duration=#{course.courseDuration},course_price=#{course.coursePrice} "
            + ",min_age=#{course.minAge},max_age=#{course.maxAge},classroom_id=#{course.classroomName},teacher_id=#{course.teacherName} "
            + "where course_id = #{course.courseId} ")
    void updateCourse(@Param("course") CourseParam courseParam);

    /**
     * 删除或恢复课程
     * @param courseId 课程id
     * @param flag 删除标志
     * @return 受影响行数
     */
    @Delete("update course set flag = #{flag} where course_id = #{courseId} ")
    Integer deleteOrRecoverCourse(@Param("courseId") Integer courseId,@Param("flag") Integer flag);

    /**
     * 批量删除或恢复删除课程
     * @param courseIds 课程id列表
     * @param flag 删除标志
     * @return 受影响行数
     */
    @Delete({"<script>","update course set flag = #{flag} where course_id in",
            "<foreach collection='list' item='courseId' open='(' separator=',' close=')'>",
            "#{courseId}",
            "</foreach>",
            "</script>"})
    Integer batchDeleteOrRecoverCourses(@Param("list") List<Integer> courseIds,@Param("flag") Integer flag);

}
